package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.VO.CartVo;
import com.example.demo.dao.CartDao;
import com.example.demo.dao.ProductDao;
import com.example.demo.model.Cart;
import com.example.demo.model.Product;
import com.example.demo.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartServiceimpl extends ServiceImpl<CartDao, Cart> implements CartService {

    @Resource
    private CartDao cartDao;
    @Resource
    private ProductDao productDao;

    public class InsertException extends Exception{
        String message;
        public InsertException(String x) {
            this.message=x;
            System.out.println(message);
        }

    }

    public class UpdateException extends Exception{
        String message;
        public UpdateException(String x) {
            this.message=x;
            System.out.println(message);
        }
    }

    public class CartNotFoundException extends Exception{
        String message;
        public CartNotFoundException(String message){
            this.message=message;
            System.out.println(message);
        }
    }

    public class AccessDeniedException extends Exception{
        String message;
        public AccessDeniedException(String message){
            this.message=message;
            System.out.println(message);
        }
    }

    @Override
    public void addToCart(Integer uid, Integer pid) throws InsertException, UpdateException {
        //查询当前要添加的这个物品是否在表中已存在
        Cart result = cartDao.findByidAndPid(uid, pid);
        if(result == null) { //表示这个商品从来没有被添加到购物车中，则进行新增操作
            Cart cart = new Cart();

            cart.setUid(uid);
            cart.setPid(pid);
            //cart.setAmount(num);
            //补全价格，来自于商品中的数据
/*            Product product = productDao.findById(pid);
            cart.setPrice(product.getPrice());*/

            Integer rows = cartDao.insert(cart);
            if(rows != 1) {
                throw new InsertException("插入数据时产生未知的异常");
            }
        } else { //表示当前的商品在购物车中已经存在，则更新到这条数据的num值
            Integer number = result.getAmount()+1; /*+ num*/;
            Integer rows = cartDao.updateNumByCid(result.getCid(), number);
            if(rows != 1) {
                throw new UpdateException("更新数据时产生未知的异常");
            }
        }
    }

    @Override
    public List<CartVo> getVOByUid(Integer uid) {
        return cartDao.findVOByUid(uid);
    }

    @Override
    public Integer addNum(Integer cid, Integer uid) throws CartNotFoundException, AccessDeniedException, UpdateException {
        Cart result = cartDao.findByCid(cid);
        if(result == null) {
            throw new CartNotFoundException("数据不存在");
        }
        if(result.getUid()!=uid) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getAmount() + 1;
        Integer rows = cartDao.updateNumByCid(cid, num);
        if(rows != 1) {
            throw new UpdateException("更新数据失败！");
        }
        return num;
    }

    @Override
    public Integer reduceNum(Integer cid, Integer uid) throws CartNotFoundException, AccessDeniedException, UpdateException {
        Cart result = cartDao.findByCid(cid);
        if(result == null) {
            throw new CartNotFoundException("数据不存在");
        }
        if(result.getUid()!=uid) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = result.getAmount() - 1;
        Integer rows = cartDao.updateNumByCid(cid, num);
        if(rows != 1) {
            throw new UpdateException("更新数据失败！");
        }
        return num;
    }

    @Override
    public void deleteItem(Integer cid, Integer uid) throws UpdateException {
        Integer rows=cartDao.deleteItem(cid,uid);
        if(rows != 1) {
            throw new UpdateException("更新数据失败！");
        }
    }

}
